package com.gitee.wsl.common.chart.render.background.axis.line

import android.graphics.Canvas
import android.graphics.Paint
import com.gitee.wsl.common.chart.ChartEnum
import com.gitee.wsl.common.chart.render.background.axis.model.PlotAxisTick
import com.gitee.wsl.mathematics.geometry.shape.Rectangle


class CategoryLineAxis: BaseLineAxis() {

    init {
        tickLabelPaint.textAlign= Paint.Align.CENTER
    }


    override fun renderClipAxisClosed(
        canvas: Canvas,
        plotArea: Rectangle,
        axisLocation: ChartEnum.AxisLocation
    ) {
        val plotLeft: Float = plotArea.left
        val plotTop: Float = plotArea.top
        val plotRight: Float = plotArea.right
        val plotBottom: Float = plotArea.bottom
        when (axisLocation) {
            ChartEnum.AxisLocation.LEFT, ChartEnum.AxisLocation.RIGHT, ChartEnum.AxisLocation.VERTICAL_CENTER -> {
                drawAxisLine(canvas, plotLeft, plotBottom, plotLeft, plotTop)
                drawAxisLine(canvas, plotRight, plotBottom, plotRight, plotTop)
            }

            ChartEnum.AxisLocation.TOP, ChartEnum.AxisLocation.BOTTOM, ChartEnum.AxisLocation.HORIZONTAL_CENTER -> {
                drawAxisLine(canvas, plotLeft, plotTop, plotRight, plotTop)
                drawAxisLine(canvas, plotLeft, plotBottom, plotRight, plotBottom)
            }
        }
    }

    override fun renderAxisLabels(
        canvas: Canvas,
        plotArea: Rectangle,
        lstLabels: MutableList<PlotAxisTick>,
        left: Float,
        moveX: Float,
        moveY: Float,
        axisLocation: ChartEnum.AxisLocation
    ) {
        var showTicks = true
        for (i in lstLabels.indices) {
            val t = lstLabels[i]
            when (axisLocation) {
                ChartEnum.AxisLocation.LEFT, ChartEnum.AxisLocation.RIGHT, ChartEnum.AxisLocation.VERTICAL_CENTER -> {
                    if (!t.isShowTickMarks || !isDrawYAxisTickMarks(plotArea, t.y, moveY))
                        showTicks = false
                    drawHorizontalTick(
                        left,
                        plotArea.left,
                        canvas, t.x, t.y, t.label,
                        t.labelX, t.labelY,
                        showTicks
                    )
                }

                ChartEnum.AxisLocation.TOP, ChartEnum.AxisLocation.BOTTOM, ChartEnum.AxisLocation.HORIZONTAL_CENTER -> {
                    val oe = if (i % 2 != 0) ChartEnum.ODD_EVEN.ODD else ChartEnum.ODD_EVEN.EVEN
                    if (!t.isShowTickMarks || !isDrawXAxisTickMarks(plotArea, t.x, moveX))
                        showTicks = false
                    drawVerticalTick(
                        canvas, t.x, t.y, t.label,
                        t.labelX, t.labelY,
                        showTicks, oe
                    )
                }
            }
            showTicks = true
        }
        lstLabels.clear()
    }
}